package com.aaa.service.jl.impl;

import com.aaa.dao.jl.ResultSasDao;
import com.aaa.dao.jl.TesterSasDao;
import com.aaa.pojo.TesterVo;
import com.aaa.pojo.jl.ResultSas;
import com.aaa.pojo.jl.TesterSas;
import com.aaa.service.jl.TesterSasService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TesterSasServiceImpl implements TesterSasService {
    @Autowired
    private TesterSasDao testerSasDao;
    @Autowired
    private ResultSasDao resultSasDao;
    @Override
    public List<TesterSas> findAll() {
        List<TesterSas> testerSas = testerSasDao.fidnAll();
        return testerSas;
    }

    @Override
    public List<TesterSas> findAll(String page, String limit) {
        int pageInt = Integer.parseInt(page);
        int limitInt = Integer.parseInt(limit);
        int statr = (pageInt - 1) * limitInt;
        List<TesterSas> all1 = testerSasDao.findAll1(statr,limitInt);
        return all1;
    }

    @Override
    public int findCount() {
        return testerSasDao.findCount();
    }

    @Override
    public int deleteId(String idList) {
        return testerSasDao.deleteId(Integer.parseInt(idList));
    }

    @Override
    public int deleteList(List<String> idList) {
        return testerSasDao.deleteList(idList);
    }

    @Override
    public List<Map<String, Object>> phoneFpa(String phone) {
        return testerSasDao.phoneFpa(phone);
    }

    @Override
    public List<Map<String, Object>> phoneSas(String phone) {
        return testerSasDao.phoneSas(phone);
    }

    @Override
    public List<Map<String, Object>> phoneSds(String phone) {
        return testerSasDao.phoneSds(phone);
    }

    @Override
    public String saveTestResult(List<Map<String, Object>> answerList, TesterVo tester) {
        TesterSas testerSas =new TesterSas();
        BeanUtils.copyProperties(tester,testerSas,"id");
        testerSasDao.insert(testerSas);
        int testerId = testerSas.getId();
        Integer i = insertBatch(answerList, testerId);

        if (i>0){
            return seleResult(testerId);
        }
        return null;
    }

    @Override
    public int insertBatch(List<Map<String, Object>> answerList, Integer testerId) {
        return testerSasDao.insertBatch(answerList,testerId);
    }

    @Override
    public String seleResult(Integer testerId) {
        ResultSas resultSas = resultSasDao.selectResultSas(testerId);
        double a =resultSas.getForward()+ resultSas.getInversion();
        if (a <= 50.0) {
            return "/SasNormal.jsp";
        } else if (a < 60.0) {
            return "/SasMild.jsp";
        } else {
            return a < 70.0 ? "/SasModerate.jsp" : "/SasSevere.jsp";
        }
    }
}


